package com.mtu.parser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
//import java.io.File;
//import java.util.Iterator;

/**
 * Example program to list tables from a URL and storing each table in an array named arrOfTables[]
 * Then the data of first table is stored in an array named personalInfo[]
 * This code can be further improved by removing hard coding in some cases. 
 */

public class TableParserNet2 
{
	public static void main(String[] args) throws IOException 
	{
		int i=0,j=0;
         
		String url="http://www.uptu.ac.in/results/EVEN_SEMESTER_10_11/bte6_10_11.asp?rollno=0806410005";

		Document doc = Jsoup.connect(url).get(); 
		Elements table = doc.select("table");
        
    	int size=table.size();
    	//System.out.println(size);
        
    	Element [] arrOfTables = new Element[size];
    	
    	for (Element src : table) 
        {
    		arrOfTables[i]=src;   //Data is stored from arrOfTables[1] onwards.  
    		i++;		
        }
        //System.out.println(arrOfTables[1].text());
    	
    	/* This section of code scraps the personal information
    	 * The personal information is stored in the arrOfTables[1]
    	 * Then student's name, father's name etc are extracted*/
    	
    	Elements firstTable=arrOfTables[1].select("td");
    	//System.out.println(firstTable);
    	
    	int firstTableSize=firstTable.size();
    	//System.out.println(firstTableSize);
    	
    	String[] personalInfo= new String[firstTableSize];
    	
    	for (Element src : firstTable) 
        {
    		personalInfo[j]=src.text().trim();   //Data is stored from tableData[0] onwards.  
    		j++;		
        }
    	//System.out.println(personalInfo[1]);
    	
    	String name = null,fatherName = null,rollNo = null,status = null,branch = null,college = null;
    	
    	for(i=0;i<firstTableSize;i=i+2)
    	{
    		if (personalInfo[i].equalsIgnoreCase("Name:"))
    		{
    			name=personalInfo[i+1];
    			continue;
    		}
    		
    		if(personalInfo[i].equalsIgnoreCase("Fathers Name:"))
    		{
    			fatherName=personalInfo[i+1];
    			continue;
    		}
    		
    		if(personalInfo[i].equalsIgnoreCase("Roll No:"))
    		{
    			rollNo=personalInfo[i+1];
    			continue;
    		}
    		
    		if(personalInfo[i].equalsIgnoreCase("Status:"))
    		{
    			status=personalInfo[i+1];
    			continue;
    		}
    		
    		if(personalInfo[i].equalsIgnoreCase("Course/Branch:"))
    		{
    			branch=personalInfo[i+1];
    			continue;
    		}
    		
    		if(personalInfo[i].equalsIgnoreCase("Institute Name"))
    		{
    			college=personalInfo[i+1];
    			continue;
    		}
    	}
    	System.out.println("Name= "+name);	
    	System.out.println("Father's Name= "+fatherName);
    	System.out.println("Roll no= "+rollNo); 	
    	System.out.println("Status= "+status);
    	System.out.println("Branch= "+branch);
    	System.out.println("College= "+college);	
	}
}